package com.music.order.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.music.common.core.constant.Constants;
import com.music.common.core.constant.SecurityConstants;
import com.music.common.core.domain.R;
import com.music.common.core.enums.ExceptionCode;
import com.music.common.core.exception.CustomException;
import com.music.common.core.utils.SnowflakeIdWorker;
import com.music.common.core.utils.StringUtils;
import com.music.order.client.UserFeign;
import com.music.order.dto.OrderDto;
import com.music.order.vo.OrderVo;
import com.music.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import com.music.order.mapper.OrderMapper;
import com.music.order.domain.Order;
import com.music.order.service.OrderService;

import javax.annotation.Resource;

/**
 * 订单列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-22
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserFeign userFeign;



    /**
     * 查询订单列表
     *
     * @param orderId 订单列表主键
     * @return 订单列表
     */
    @Override
    public Order selectOrderByOrderId(Long orderId) {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表列表
     *
     * @param order 订单列表
     * @return 订单列表
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单列表
     *
     * @param order 订单列表
     * @return 结果
     */
    @Override
    public int insertOrder(Order order) {
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单列表
     *
     * @param order 订单列表
     * @return 结果
     */
    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单列表
     *
     * @param orderIds 需要删除的订单列表主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderIds(Long[] orderIds) {
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单列表信息
     *
     * @param orderId 订单列表主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderId(Long orderId) {
        return orderMapper.deleteOrderByOrderId(orderId);
    }

    @Override
    public List<OrderVo> selectOrderVoList(Order order) {
        return orderMapper.selectOrderVoList(order);
    }

    @Override
    public int deleteByOrderId(Long orderId) {
        Order order = new Order();

        order.setOrderId(orderId);
        order.setOrderDelete(1);
        return orderMapper.updateOrder(order);
    }

    @Override
    public String snowFlakeIdGenerate() {
        Long snowId = SnowflakeIdWorker.generateId();
        String orderId = String.valueOf(SnowflakeIdWorker.generateId());
        //存入redis
        redisTemplate.opsForValue().set(orderId, orderId, 15, TimeUnit.MINUTES);
        return orderId;
    }

    @Override
    public Long activate(String activateUserAccount) {
        //根据布隆过滤器判断是否存在用户账号
        String script = "return tonumber(redis.call(\"bf.exists\", KEYS[1], ARGV[1]))";

        Long result = stringRedisTemplate.opsForValue()
                .getOperations()
                .execute(new DefaultRedisScript<>(script, Long.class),
                        new ArrayList<String>() {{
                            add(Constants.WHITE_USER_ACCOUNT_KEY);
                        }}, activateUserAccount
                );
        if (result.intValue() == 1) {
            // 查询数据库去除activateUserId并且排除 验证布隆过滤器是否误判
            Long activateUserId = userFeign.findActivateUserByAccount(activateUserAccount);
            if (activateUserId == null||activateUserId==0) {
                throw new CustomException(ExceptionCode.INVALID_ACTIVATE_USER_ACCOUNT);
            }else {
                return activateUserId;
            }
        } else {
            throw new CustomException(ExceptionCode.INVALID_ACTIVATE_USER_ACCOUNT);
        }

    }

    @Override
    public Boolean isReal(Long OrderId) {
        String orderId = String.valueOf(OrderId);
        String orderIdRedis = (String) redisTemplate.opsForValue().get(orderId);
        if (StringUtils.isEmpty(orderIdRedis) || !StringUtils.equals(orderId, orderIdRedis)) {
            throw new CustomException(ExceptionCode.ERROR_ORDERID);
        }
        return true;
    }

    @Override
    public void saleMusic(OrderDto orderDto) {

    }
}
